#include "ComparableRocksdb.h"

#include "Rocksdb.h"
#include "OptionsArgs.h"
#include "rocksdb/comparator.h"

namespace helper {
namespace demo {

std::unique_ptr<ComparableRocksdb> ComparableRocksdb::Open(IRocksdb<key_type> & db, const comparator_type * comparator){
  if(comparator == nullptr){
    comparator = ::rocksdb::BytewiseComparator();
  }
  auto cdb = std::unique_ptr<ComparableRocksdb>(new ComparableRocksdb(db, *comparator));
  if(!cdb->open()){ 
    cdb.reset();
  }
  return cdb;
}

ComparableRocksdb::ComparableRocksdb(IRocksdb<key_type> & db, const comparator_type &  comparator)
  : comparator_(comparator)
    , db_(db)
{
}
ComparableRocksdb::~ComparableRocksdb() {
}


const std::string & ComparableRocksdb::name() const {
  return db_.name();
}

const OptionsArgs & ComparableRocksdb::optionsArgs() const {
  return db_.optionsArgs();
}
OptionsArgs & ComparableRocksdb::optionsArgs()   {
  return db_.optionsArgs();
}

IDBInstance::DBImpl * ComparableRocksdb::instance()  {
  return db_.instance();
}

Status ComparableRocksdb::put(const key_type & key, const value_type & value)  {
  return db_.put(key, value);
}
Status ComparableRocksdb::get(const key_type & key, std::string & value) const {
  return db_.get(key, value);
}
Status ComparableRocksdb::del(const key_type & key)  {
  return db_.del(key);
}
bool ComparableRocksdb::foreach(const visitor & v, const key_type & start, const key_type & end) const  {
  return db_.foreach(v, start, end);
}
ComparableRocksdb::iterator * ComparableRocksdb::range(const key_type& start, const key_type& end) const  {
  return db_.range(start, end);
}


Status ComparableRocksdb::put(const WriteOptions & write, const key_type & key, const value_type & value)  {
  return db_.put(write, key, value);
}
Status ComparableRocksdb::get(const ReadOptions & read, const key_type & key, std::string & value) const  {
  return db_.get(read, key, value);
}
Status ComparableRocksdb::del(const WriteOptions & write, const key_type & key)  {
  return db_.del(write, key);
}
bool ComparableRocksdb::foreach(const ReadOptions& read, const visitor & v, const key_type& start, const key_type & end) const  {
  return db_.foreach(read, v, start, end);
}

ComparableRocksdb::iterator * ComparableRocksdb::range(const ReadOptions & read, const key_type& start, const key_type& end) const  {
  return db_.range(read, start, end);
}


bool ComparableRocksdb::_open() {
  db_.optionsArgs().open().comparator = &comparator_;
  return db_.open();
}
bool ComparableRocksdb::_close()  {
  return db_.close();
}

}//namespace demo
}//namespace helper
